package com.example.housekeeping.controller;

import com.example.housekeeping.pojo.dto.OrdersAddNewDTO;
import com.example.housekeeping.pojo.dto.OrdersUpdateDTO;
import com.example.housekeeping.pojo.dto.WorkerAddNewDTO;
import com.example.housekeeping.pojo.dto.WorkerUpdateDTO;
import com.example.housekeeping.pojo.entity.Orders;
import com.example.housekeeping.pojo.vo.OrdersListItemVO;
import com.example.housekeeping.pojo.vo.WorkerListItemVO;
import com.example.housekeeping.service.OrdersService;
import com.example.housekeeping.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import java.util.List;

/**
 * 订单(Orders)表控制层
 *
 * @author Xhe
 * @since 2023-03-30 14:26:07
 */
@Slf4j
@RestController
@RequestMapping("/orders")
@Api(tags = "06.订单管理模块")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    public OrdersController(){
        log.debug("创建Orders控制器对象:OrdersController");
    }

    @ApiOperation("添加订单")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(OrdersAddNewDTO ordersAddNewDTO) {
        log.debug("开始处理【添加订单】的请求，参数：{}", ordersAddNewDTO);
        ordersService.addNew(ordersAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据ID删除订单")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除订单】的请求，参数：{}", id);
        ordersService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据ID修改订单")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id, @Valid OrdersUpdateDTO ordersUpdateDTO) {
        log.debug("开始处理【根据ID修改订单】的请求，参数：{},新数据:{}", id,ordersUpdateDTO);
        ordersService.updateById(id,ordersUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<OrdersListItemVO>> list() {
        log.debug("开始处理【查询订单列表】的请求，无参数");
        List<OrdersListItemVO> list = ordersService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/getNotFinishByWorkerId")
    @ApiOperation("根据服务员id查询未完成订单业务")
    @ApiOperationSupport(order = 430)
    public JsonResult<Orders> getNotFinishByWorkerId(Long workerId){
        log.debug("开始处理【根据服务员id查询未完成订单】的请求，参数:{}",workerId);
        Orders notFinishByWorkerId = ordersService.getNotFinishByWorkerId(workerId);
        return JsonResult.ok(notFinishByWorkerId);
    }

}

